package cn.yinyu.queue.module.ecg.service.jobrecord;

import cn.yinyu.queue.module.ecg.controller.admin.appointment.vo.AppointmentRespVO;
import cn.yinyu.queue.module.ecg.dal.dataobject.jobrecord.JobRecordStatisticDO;
import cn.yinyu.queue.module.ecg.dal.mysql.devrent.DevRentMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.yinyu.queue.module.ecg.controller.admin.jobrecord.vo.*;
import cn.yinyu.queue.module.ecg.dal.dataobject.jobrecord.JobRecordDO;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.pojo.PageParam;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;

import cn.yinyu.queue.module.ecg.dal.mysql.jobrecord.JobRecordMapper;

import javax.annotation.Resource;

import static cn.yinyu.queue.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.yinyu.queue.framework.common.pojo.CommonResult.success;
import static cn.yinyu.queue.module.ecg.enums.ErrorCodeConstants.*;

/**
 * 工作记录 Service 实现类
 *
 * @author 垠域源码
 */
@Service
@Validated
public class JobRecordServiceImpl implements JobRecordService {

    @Resource
    private JobRecordMapper jobRecordMapper;

    @Resource
    private DevRentMapper devRentMapper;

    @Override
    public Long createJobRecord(JobRecordSaveReqVO createReqVO) {
        // 插入
        JobRecordDO jobRecord = BeanUtils.toBean(createReqVO, JobRecordDO.class);
        jobRecordMapper.insert(jobRecord);
        // 返回
        return jobRecord.getId();
    }

    @Override
    public void updateJobRecord(JobRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateJobRecordExists(updateReqVO.getId());
        // 更新
        JobRecordDO updateObj = BeanUtils.toBean(updateReqVO, JobRecordDO.class);
        jobRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteJobRecord(Long id) {
        // 校验存在
        validateJobRecordExists(id);
        // 删除
        jobRecordMapper.deleteById(id);
    }

    private void validateJobRecordExists(Long id) {
        if (jobRecordMapper.selectById(id) == null) {
            throw exception(JOB_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public JobRecordDO getJobRecord(Long id) {
        return jobRecordMapper.selectById(id);
    }

    @Override
    public PageResult<JobRecordDO> getJobRecordPage(JobRecordPageReqVO pageReqVO) {
        PageResult<JobRecordDO> pageResult = jobRecordMapper.selectPage(pageReqVO);

        List<JobRecordDO> jobRecordDOList = pageResult.getList();
        if ( !jobRecordDOList.isEmpty() ) {
            List<Long> rentIdList = jobRecordDOList.stream().map(item -> item.getRentId()).toList();
            List<Integer> paidInfoList = devRentMapper.getPaidInfo(rentIdList);
            for (int i = 0; i < jobRecordDOList.size(); i++) {
                jobRecordDOList.get(i).setPaid(paidInfoList.get(i));
            }
        }

        return  pageResult;
    }

    @Override
    public List<JobRecordStatisticDO> getJobStatistic(LocalDateTime jobTimeFrom, LocalDateTime jobTimeTo) {

        return jobRecordMapper.getJobStatistic(jobTimeFrom, jobTimeTo);
    }

}